// pages/guanggaoshezhi/guanggaoshezhi.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    guanggaoList: [],
    showflag: false,
  },
  //长按删除
  changan(e) {
    let that = this
    let obj = that.data.guanggaoList[e.currentTarget.dataset.index]
    console.log(555, obj)
    wx.showModal({
      title: "提示",
      content: "是否删除该条轮播",
      success(res) {
        if (res.confirm) {
          if (that.data.guanggaoList.length == 1) {
            wx.showToast({
              title: '广告管理至少要有一张！',
              icon: 'none'
            })
            return
          }
          wx.request({
            url: app.globalData.myurl,
            data: {
              cmd: "upShopImg",
              shopId: wx.getStorageSync('shopId'),
              imgId: obj.imgId,
              seq: obj.seq,
              type: 2,
              urlType: obj.urltype,
              urlValue: obj.urlvalue,
              imgUrl: obj.img,
            },
            header: {
              "content-type": "application/x-www-form-urlencoded",
              "token": wx.getStorageSync('token')
            },
            success(res) {
              if (res.data.zjstate == 1) {
                setTimeout(function () {
                  wx.hideLoading()
                  wx.stopPullDownRefresh() //停止下拉刷新
                }, 0)
                setTimeout(function () {
                  wx.showModal({
                    title: '您的账号已在其他设备登录，请重新登录！',
                    showCancel: false, //是否显示取消按钮false去掉取消按钮
                    success: function (res) {
                      if (res.confirm) {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      } else {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      }
                    }
                  })
                }, 0)
                return
              }
              setTimeout(function() {
                wx.hideLoading()
              }, 0)
              setTimeout(function() {
                wx.showToast({
                  title: res.data.resultNote,
                  icon: 'none',
                  duration: 1500
                })
              }, 0)
              console.log(res)
              if (res.data.result == 0) {
                setTimeout(function() {
                  wx.showLoading({
                    title: '加载中',
                  })
                  that.getList()
                }, 1500)
              }
            }
          });
        } else if (res.cancel) {}
      }
    });
  },
  // 手指按下
  handleTouchStart: function(e) {
    this.startTime = e.timeStamp;
  },
  //手指离开
  handleTouchEnd: function(e) {
    this.endTime = e.timeStamp;
  },
  // 去修改
  toxiugai(e) {
    let that = this
    if (that.endTime - that.startTime < 350) {
      let obj = JSON.stringify(that.data.guanggaoList[e.currentTarget.dataset.index])
      console.log(obj)
      wx.navigateTo({
        url: '/pages/setting/page/addguanggaoshezhi/addguanggaoshezhi?obj=' + obj,
      })
    }
  },
  // 添加
  addguanggao() {
    if (wx.getStorageSync('shopState') == 1) {
      wx.showToast({
        title: '店铺停用状态下，您无权进行此操作！',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.navigateTo({
      url: '/pages/setting/page/addguanggaoshezhi/addguanggaoshezhi',
    })
  },
  // 获取列表
  getList() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getShopImg",
        shopId: wx.getStorageSync('shopId'),
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
          setTimeout(function() {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
          that.setData({
            showflag: true
          })
        } else {
          that.setData({
            guanggaoList: res.data.shopList,
            showflag: true
          })
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
        }
      }
    });

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    that.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '众商联采',
      path: "/pages/login/login", // 路径，传递参数到指定页面。
      // imageUrl:'../../../images/logo/LOGO.png'
      imageUrl: app.globalData.logo
    }
  }
})